package com.hamll.order.filters;

import com.hamll.order.utils.ThreadLocalUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 29143
 */

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求头中获取uid
        String authorization = request.getHeader("authorization");
        //存取进共享变量中
        if (authorization!=null){
            ThreadLocalUtils.setUserId(Long.valueOf(authorization));
            System.out.println("订单服务拦截器已执行=>uid: "+authorization+" 已存入共享变量中...");
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
